package org.example.juc.async;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class FutureTaskExample {
    public static void main(String[] args) {
        Callable<Integer> callableTask = () -> {
            // 模拟耗时操作
            Thread.sleep(2000);
            return 42; // 返回结果
        };

        FutureTask<Integer> futureTask = new FutureTask<>(callableTask);
        Thread thread = new Thread(futureTask);
        thread.start();

        try {
            System.out.println("Waiting for result...");
            Integer result = futureTask.get(); // 获取结果，会阻塞直到任务完成
            System.out.println("Result: " + result);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
}